package utility;

import java.util.ArrayList;

import android.graphics.Bitmap;

public class Utility_Game {

<<<<<<< .mine
	/**
	 * Ham nay dung de xat dinh xem mot vi tri tren ban co co quan co nao dang
	 * chiem du hay khong
	 * 
	 * @param mPieces
	 *            trang thai hien tai cua ban co
	 * @param mPosition
	 *            vi tri tren ban co can xat dinh xem co ton tai quan co hay
	 *            khogn
	 * @return neu tai mPosition co ton tai quan co thi return lai quan co do
	 *         neu khong ton tai thi return null
=======
	// Mau co cua ban (1 la do, 2 la xanh)
	public int indexYou;

	/*
	 * Lay quan co tai vi tri (posCol,posRow). Tra ve null neu vi tri do khong
	 * co quan co nao ca.
>>>>>>> .r17
	 */
<<<<<<< .mine
	public static Piece getPieceByPosition(ArrayList<Piece> mPieces,
			Position mPosition) {
		Piece piece = null;
		for (Piece p : mPieces) {
			if (p.getPosition().getX() == mPosition.getX()
					&& p.getPosition().getY() == mPosition.getY()) {
				piece = new Piece(p);
				break;
=======
	public static Item getItem(ArrayList<Item> items, int posRow, int posCol) {
		Item item = null;
		for (Item i : items) {
			if (i.getCurrentCol() == posCol && i.getCurrentRow() == posRow) {
				item = new Item(i);
>>>>>>> .r17
			}
		}
		return piece;
	}

	/**
	 * @param mPieces
	 *            trang thai hien tai cua ban co
	 * @param mPo
	 *            vi tri quan co muon thay doi index
	 * @param index
	 *            mau cua quan co
	 * @return trang thai ban co sau khi thay doi index cua quan co
	 */
<<<<<<< .mine
	public static ArrayList<Piece> setItem(ArrayList<Piece> mPieces,
			Position mPo, int index) {
		ArrayList<Piece> Temp = new ArrayList<Piece>();
		Bitmap bittemp = null;
		for (Piece piece : mPieces) {
			Piece temp1 = new Piece(piece);
			if (piece.getIndex() == index)
				bittemp = piece.getSprite();
=======
	public static ArrayList<Item> setItem(ArrayList<Item> items, int posCol,
			int posRow, int index) {
		ArrayList<Item> Temp = new ArrayList<Item>();
		for (Item item : items) {
			Item temp1 = new Item(item);
>>>>>>> .r17
			Temp.add(temp1);
		}
<<<<<<< .mine
		for (Piece i : Temp) {
			if (i.getPosition().equals(mPo)) {
				i.setIndex(index);
				i.setSprite(bittemp);
=======
		for (Item i : Temp) {
			if (i.getCurrentCol() == posCol && i.getCurrentRow() == posRow) {
				i.setIndex(index);
>>>>>>> .r17
			}
		}
		return Temp;
	}

<<<<<<< .mine
	/**
	 * @param mPieces
	 *            trang thai hien tai cua ban co
	 * @param mPiece
	 *            quan co can tim cac nuoc di
	 * @return tap hop cac nuoc di co the tu mPiece
=======
	/*
	 * Tao cac vi tri co the di tu vi tri (curSelectedColYou, curSelectedRowYou)
	 * 
	 * moi viet ma chua xu ly cho neu gap phai truong hop mo ganh cua doi phuong
	 * 
	 * con mot so van de nua
>>>>>>> .r17
	 */
<<<<<<< .mine
	public static ArrayList<Position> getDest(ArrayList<Piece> mPieces,
			ArrayList<Position> positionList, Piece mPiece) {
		ArrayList<Position> mItemsDest;
		mItemsDest = new ArrayList<Position>();
		for (int i = mPiece.getPosition().getX() - 1; i <= mPiece.getPosition()
				.getX() + 1; i++) {
			for (int j = mPiece.getPosition().getY() - 1; j <= mPiece
					.getPosition().getY() + 1; j++) {
=======
	public static ArrayList<Item> getItemsDest(ArrayList<Item> Items,
			int curSelectedRowYou, int curSelectedColYou) {
		ArrayList<Item> ItemsDest = null;
		ItemsDest = new ArrayList<Item>();
		Item item;
		for (int i = curSelectedColYou - 1; i <= curSelectedColYou + 1; i++) {
			for (int j = curSelectedRowYou - 1; j <= curSelectedRowYou + 1; j++) {
>>>>>>> .r17
				if (i >= 0 && i <= 4 && j >= 0 && j <= 4) {
<<<<<<< .mine
					if (((mPiece.getPosition().getX() + mPiece.getPosition()
							.getY()) % 2 == 0)
							|| i == mPiece.getPosition().getX()
							|| j == mPiece.getPosition().getY()) {
						for (Position pos : positionList)
							if (pos.getX() == i && pos.getY() == j) {
								Position po = new Position(pos);
								if (getPieceByPosition(mPieces, po) == null) {
									mItemsDest.add(po);
								}
							}
=======
					if (((curSelectedColYou + curSelectedRowYou) % 2 == 0)
							|| i == curSelectedColYou || j == curSelectedRowYou) {
						if (getItem(Items, j, i) == null) {
							item = new Item();
							item.setCurrentCol(i);
							item.setCurrentRow(j);
							ItemsDest.add(item);
						}
>>>>>>> .r17
					}
				}
			}
		}
<<<<<<< .mine
		return mItemsDest;
=======
		return ItemsDest;
>>>>>>> .r17
	}

<<<<<<< .mine
	public static void move(Piece piece, Position from, Position to) {

	}

=======
>>>>>>> .r17
	/*
	 * Kiem tra ganh tai 2 o (item1Col,item1Row) va (item2Col,item2Row)
	 */
	/*
<<<<<<< .mine
	 * public static ArrayList<Piece> refreshMovingHandler(ArrayList<Piece>
	 * mPieces, Position mPo1, Position mPo2) { ArrayList<Piece> Temp = new
	 * ArrayList<Piece>(); for (Piece piece : mPieces) { Piece temp1 = new
	 * Piece(piece); Temp.add(temp1); } int toCol = (item1Col + item2Col) / 2;
	 * int toRow = (item1Row + item2Row) / 2; if (item1Col >= 0 && item1Row >= 0
	 * && item2Col <= 4 && item2Row <= 4) { Item item1 = getItem(Temp, item1Col,
	 * item1Row); Item item2 = getItem(Temp, item2Col, item2Row); if (item1 !=
	 * null && item2 != null) { Item item0 = getItem(Temp, toCol, toRow); if
	 * ((item1.getIndex() == item2.getIndex()) && item0.getIndex() !=
	 * item2.getIndex()) { Temp = setItem(Temp, item1Col, item1Row,
	 * item0.getIndex()); Temp = setItem(Temp, item2Col, item2Row,
	 * item0.getIndex()); } } } return Temp; }
	 * 
	 * 
	 * Loai bo nhung quan co bi ket
	 * 
	 * public static ArrayList<Item> refreshChessBlocked(ArrayList<Item> items,
	 * int curCol, int curRow) { ArrayList<Item> itemsTemp = new
	 * ArrayList<Item>(); boolean existItem = false; ArrayList<Item> Temp = new
	 * ArrayList<Item>(); for (Item item : items) { Item temp1 = new Item(item);
	 * Temp.add(temp1); } Item curItem = getItem(Temp, curCol, curRow); boolean
	 * is_move; for (Item item : Temp) { is_move = true; if
	 * (!item.isAlly(curItem)) { existItem = true; for (int i =
	 * item.getCurrentCol() - 1; i <= item .getCurrentCol() + 1; i++) { for (int
	 * j = item.getCurrentRow() - 1; j <= item .getCurrentRow() + 1; j++) { if
	 * (i >= 0 && i <= 4 && j >= 0 && j <= 4) { if (((item.getCurrentCol() +
	 * item.getCurrentRow()) % 2 == 0) || i == item.getCurrentCol() || j ==
	 * item.getCurrentRow()) { if (getItem(Temp, i, j) == null) { is_move =
	 * false; break; } } } } } } if (is_move) itemsTemp.add(item); } if
	 * (existItem && itemsTemp.size() != Temp.size()) { return
	 * refreshChessBlocked(itemsTemp, curCol, curRow); } else { return
	 * itemsTemp; } }
	 * 
	 * 
=======
	 * Loai bo nhung quan co  bi ket
	 */
	public static ArrayList<Item> refreshChessBlocked(ArrayList<Item> items,
			int curCol, int curRow) {
		ArrayList<Item> itemsTemp = new ArrayList<Item>();
		boolean existItem = false;
		ArrayList<Item> Temp = new ArrayList<Item>();
		for (Item item : items) {
			Item temp1 = new Item(item);
			Temp.add(temp1);
		}
		Item curItem = getItem(Temp, curCol, curRow);
		boolean is_move;
		for (Item item : Temp) {
			is_move = true;
			if (!item.isAlly(curItem)) {
				existItem = true;
				for (int i = item.getCurrentCol() - 1; i <= item
						.getCurrentCol() + 1; i++) {
					for (int j = item.getCurrentRow() - 1; j <= item
							.getCurrentRow() + 1; j++) {
						if (i >= 0 && i <= 4 && j >= 0 && j <= 4) {
							if (((item.getCurrentCol() + item.getCurrentRow()) % 2 == 0)
									|| i == item.getCurrentCol()
									|| j == item.getCurrentRow()) {
								if (getItem(Temp, i, j) == null) {
									is_move = false;
									break;
								}
							}
						}
					}
				}
			}
			if (is_move)
				itemsTemp.add(item);
		}
		if (existItem && itemsTemp.size() != Temp.size()) {
			return refreshChessBlocked(itemsTemp, curCol, curRow);
		} else {
			return itemsTemp;
		}
	}

	/*
>>>>>>> .r17
	 * Kiem tra nuoc di tu vi tri (fromCol,fromRow) co mo ganh hay khong?
<<<<<<< .mine
	 * 
	 * public static boolean checkLoad(ArrayList<Item> items, int fromCol, int
	 * fromRow, int toCol, int toRow) { boolean isLoad = false;
	 * 
	 * ArrayList<Item> itemsTemp=new ArrayList<Item>(); for (Item item : items)
	 * { Item temp1 = new Item(item); itemsTemp.add(temp1); }
	 * 
	 * Item item1 = getItem(items, toCol, toRow); for (int i = fromCol - 1; i <=
	 * fromCol + 1; i++) { for (int j = fromRow - 1; j <= fromRow + 1; j++) { if
	 * ((fromCol + fromRow) % 2 == 0 || i == fromCol || j == fromRow) { Item
	 * item2 = getItem(items, i, j); if (item2 != null &&
	 * (!item1.isAlly(item2))) { isLoad = true; break; } } } if (isLoad) break;
	 * }
	 * 
	 * if (isLoad) { isLoad = false; if ((fromCol + fromRow) % 2 == 0) { if
	 * ((item1.isAlly(getItem(items, fromCol - 1, fromRow - 1)) && item1
	 * .isAlly(getItem(items, fromCol + 1, fromRow + 1))) ||
	 * (item1.isAlly(getItem(items, fromCol - 1, fromRow + 1)) &&
	 * item1.isAlly(getItem(items, fromCol + 1, fromRow - 1)))) { isLoad = true;
	 * } } if ((!isLoad) && ((item1.isAlly(getItem(items, fromCol, fromRow - 1))
	 * && item1 .isAlly(getItem(items, fromCol, fromRow + 1))) || (item1
	 * .isAlly(getItem(items, fromCol - 1, fromRow)) && item1
	 * .isAlly(getItem(items, fromCol + 1, fromRow))))) { isLoad = true; } } //
	 * itemsTemp=null; return isLoad; }
	 * 
	 * 
=======
	 */
	public static boolean checkLoad(ArrayList<Item> items, int fromCol,
			int fromRow, int toCol, int toRow) {
		boolean isLoad = false;
		/*
		 * ArrayList<Item> itemsTemp=new ArrayList<Item>(); for (Item item :
		 * items) { Item temp1 = new Item(item); itemsTemp.add(temp1); }
		 */
		Item item1 = getItem(items, toCol, toRow);
		for (int i = fromCol - 1; i <= fromCol + 1; i++) {
			for (int j = fromRow - 1; j <= fromRow + 1; j++) {
				if ((fromCol + fromRow) % 2 == 0 || i == fromCol
						|| j == fromRow) {
					Item item2 = getItem(items, i, j);
					if (item2 != null && (!item1.isAlly(item2))) {
						isLoad = true;
						break;
					}
				}
			}
			if (isLoad)
				break;
		}

		if (isLoad) {
			isLoad = false;
			if ((fromCol + fromRow) % 2 == 0) {
				if ((item1
						.isAlly(getItem(items, fromCol - 1, fromRow - 1)) && item1
						.isAlly(getItem(items, fromCol + 1, fromRow + 1)))
						|| (item1.isAlly(getItem(items, fromCol - 1,
								fromRow + 1)) && item1.isAlly(getItem(
								items, fromCol + 1, fromRow - 1)))) {
					isLoad = true;
				}
			}
			if ((!isLoad)
					&& ((item1
							.isAlly(getItem(items, fromCol, fromRow - 1)) && item1
							.isAlly(getItem(items, fromCol, fromRow + 1))) || (item1
							.isAlly(getItem(items, fromCol - 1, fromRow)) && item1
							.isAlly(getItem(items, fromCol + 1, fromRow))))) {
				isLoad = true;
			}
		}
		// itemsTemp=null;
		return isLoad;
	}

	/*
>>>>>>> .r17
	 * Di chuyen quan co tu vi tri (fromCol,fromRow) den vi tri (toCol,toRow)
	 * 
	 * public static ArrayList<Item> moveItem(ArrayList<Item> items, int
	 * fromCol, int fromRow, int toCol, int toRow) { ArrayList<Item> Temp = new
	 * ArrayList<Item>(); for (Item item : items) { Item temp1 = new Item(item);
	 * Temp.add(temp1); } for (Item i : Temp) { if (i.getCurrentCol() == fromCol
	 * && i.getCurrentRow() == fromRow) { i.setPreviousCol(i.getCurrentCol());
	 * i.setPreviousRow(i.getCurrentRow()); i.setCurrentCol(toCol);
	 * i.setCurrentRow(toRow); } }
	 * 
	 * if ((toCol + toRow) % 2 == 0) { Temp = refreshMovingHandler(Temp, toCol -
	 * 1, toRow - 1, toCol + 1, toRow + 1); Temp = refreshMovingHandler(Temp,
	 * toCol - 1, toRow + 1, toCol + 1, toRow - 1); } Temp =
	 * refreshMovingHandler(Temp, toCol, toRow - 1, toCol, toRow + 1); Temp =
	 * refreshMovingHandler(Temp, toCol - 1, toRow, toCol + 1, toRow);
	 * 
	 * Item curItem = getItem(Temp, toCol, toRow); ArrayList<Item> itemsTemp =
	 * refreshChessBlocked(Temp, toCol, toRow); if (itemsTemp != null) { for
	 * (Item item : itemsTemp) { if (!item.isAlly(curItem)) { Temp =
	 * setItem(Temp, item.getCurrentCol(), item.getCurrentRow(),
	 * curItem.getIndex()); } } } return Temp; }
	 */
<<<<<<< .mine
=======
	public static ArrayList<Item> moveItem(ArrayList<Item> items, int fromCol,
			int fromRow, int toCol, int toRow) {
		ArrayList<Item> Temp = new ArrayList<Item>();
		for (Item item : items) {
			Item temp1 = new Item(item);
			Temp.add(temp1);
		}
		for (Item i : Temp) {
			if (i.getCurrentCol() == fromCol && i.getCurrentRow() == fromRow) {
				i.setPreviousCol(i.getCurrentCol());
				i.setPreviousRow(i.getCurrentRow());
				i.setCurrentCol(toCol);
				i.setCurrentRow(toRow);
			}
		}

		if ((toCol + toRow) % 2 == 0) {
			Temp = refreshMovingHandler(Temp, toCol - 1, toRow - 1, toCol + 1,
					toRow + 1);
			Temp = refreshMovingHandler(Temp, toCol - 1, toRow + 1, toCol + 1,
					toRow - 1);
		}
		Temp = refreshMovingHandler(Temp, toCol, toRow - 1, toCol, toRow + 1);
		Temp = refreshMovingHandler(Temp, toCol - 1, toRow, toCol + 1, toRow);

		Item curItem = getItem(Temp, toCol, toRow);
		ArrayList<Item> itemsTemp = refreshChessBlocked(Temp, toCol, toRow);
		if (itemsTemp != null) {
			for (Item item : itemsTemp) {
				if (!item.isAlly(curItem)) {
					Temp = setItem(Temp, item.getCurrentCol(),
							item.getCurrentRow(), curItem.getIndex());
				}
			}
		}
		return Temp;
	}

>>>>>>> .r17
	/**
	 * @param x
	 *            toa do x tuyet doi tren man hinh
	 * @param y
	 *            toa do y tuyet doi tren man hinh
	 * @param itemchess
	 *            quan co:
	 * @param mPoList
	 *            danh sach cac vi tri cua quan co
	 * @return null neu khong touch vao 1 diem tren ban co hoac vi tri tren ban
	 *         co ma touch vao
	 */
<<<<<<< .mine
	public static Position getPosition(ArrayList<Position> mPoList,
			Chess itemchess, float x, float y) {
		Position mPosition = null;
=======
	public static Position getPosition(float x, float y, Chess itemchess,
			ArrayList<Position> mPoList) {
		Position mPosition = null;
>>>>>>> .r17
		for (Position mPo : mPoList)
			if ((mPo.getxPost() >= (x - itemchess.getWidth() / 2))
					&& (mPo.getxPost() <= (x + itemchess.getWidth() / 2))
					&& (mPo.getyPost() >= (y - itemchess.getHeight() / 2))
					&& (mPo.getyPost() <= (y + itemchess.getHeight() / 2))) {
				mPosition = new Position();
				mPosition.setX(mPo.getX());
				mPosition.setY(mPo.getY());
				mPosition.setxPost(mPo.getxPost());
				mPosition.setyPost(mPo.getyPost());
			}
		return mPosition;
	}

	/**
	 * @param board
	 *            la ban co
	 * @return
	 */
	public static ArrayList<Position> InitPosition(Chess board) {
		int cellWidth = (board.getWidth() - 2) / 4;
		int cellHeight = (board.getHeight() - 2) / 4;
		ArrayList<Position> mPositions = new ArrayList<Position>();
		for (int i = 0; i < 5; i++)
			for (int j = 0; j < 5; j++) {
				Position position = new Position();
				position.setxPost(board.getLeft() + j * cellWidth);
				position.setyPost(board.getTop() + i * cellHeight);
				position.setX(i);
				position.setY(j);
				mPositions.add(position);
			}
		return mPositions;
	}

}
